package project2.util;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * 图片导入工具
 * 为需要导入的图片创建对象
 * 获取图片中部分区域的内容
 */
public class ImageLoader {

    private BufferedImage sourceImg;

    public ImageLoader(String imagePath) throws IOException {
        sourceImg = ImageIO.read(new File(imagePath));
    }

    public Image getImage(int posX, int posY, int width, int height) {
        BufferedImage targetImg = this.sourceImg.getSubimage(posX, posY, width, height);
        Image img = new ImageIcon(targetImg).getImage();
        return img;
    }

}
